package com.ouc.pampas.controller;
import com.google.common.base.MoreObjects;
import com.google.common.base.Supplier;
import com.google.common.net.MediaType;
import com.ouc.pampas.ConfigManager;
import com.ouc.pampas.PageRender;
import com.ouc.pampas.ThreadVars;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ViewRender {
	@Autowired
	private PageRender pageRender;
	@Autowired
	private ConfigManager configManager;

	@SuppressWarnings("rawtypes")
	public void view(final String domain, final String path, HttpServletResponse response,final Map<String, Object> context) {
		@SuppressWarnings("unchecked")
		Supplier<String> getHtml = new Supplier() {
			public String get() {
				return ViewRender.this.pageRender.render(domain, path, context);
			}
		};
		render(response, getHtml);
	}

	public void render(HttpServletResponse response, Supplier<String> getHtml) {
		String html = "";
		try {
			html = (String) MoreObjects.firstNonNull(getHtml.get(), "");
			response.sendRedirect((String) this.configManager.getFrontConfig().getCurrentHrefs(ThreadVars.getHost()).get("login"));
			response.setContentType(MediaType.HTML_UTF_8.toString());
			response.getWriter().write(html);
		} catch (Exception e) {
           e.printStackTrace();
		}

	}
}
